package cn.yilongquan.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.yilongquan.mapper.EmployeeRoleMapper;
import cn.yilongquan.user.domain.Employee;
import cn.yilongquan.mapper.EmployeeMapper;
import cn.yilongquan.user.query.EmployeeQuery;
import cn.yilongquan.service.IEmployeeService;
import cn.yilongquan.util.R;
import cn.yilongquan.util.RandomStringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper; // 新增注入

    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    @Override
    public void add(Employee employee) {
        // 密码加盐加密
        String salt = RandomStringUtils.generateSalt(10);
        employee.setSalt(salt);
        employee.setPassword(SecureUtil.md5(employee.getPassword() + salt));
        // 默认状态为启用
        employee.setState(1);
        employeeMapper.add(employee);
        // 处理角色关联
        if (employee.getRoleIds() != null && !employee.getRoleIds().isEmpty()) {
            employeeRoleMapper.insertBatch(employee.getId(), employee.getRoleIds());
        }
    }

    @Override
    public void update(Employee employee) {
        // 如果更新密码，需要重新加密（此处简化处理，实际需判断密码是否变更）
        if (employee.getPassword() != null && !employee.getPassword().isEmpty()) {
            String salt = RandomStringUtils.generateSalt(10);
            employee.setSalt(salt);
            employee.setPassword(SecureUtil.md5(employee.getPassword() + salt));
        }
        employeeMapper.update(employee);

        // 1. 删除旧角色关联
        employeeRoleMapper.deleteByEmployeeId(employee.getId());

        // 2. 插入新角色关联
        if (employee.getRoleIds() != null && !employee.getRoleIds().isEmpty()) {
            employeeRoleMapper.insertBatch(employee.getId(), employee.getRoleIds());
        }

        // 3. 更新员工信息
        employeeMapper.update(employee);
    }

    @Override
    public void deleteById(Long id) {
        employeeRoleMapper.deleteByEmployeeId(id); // 删关联
        employeeMapper.deleteById(id); // 删员工
    }

    @Override
    public void batchDelete(List<Long> ids) {
        ids.forEach(employeeId -> employeeRoleMapper.deleteByEmployeeId(employeeId));
        employeeMapper.batchDelete(ids);
    }

    @Override
    public R pageList(EmployeeQuery query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        List<Employee> employees = employeeMapper.pageList(query);
        return R.ok(new PageInfo<>(employees));
    }
    // 查询员工已关联的角色ID（供编辑回显）
    @Override
    public List<Long> findRoleIdsByEmployeeId(Long employeeId) {
        return employeeRoleMapper.findRoleIdsByEmployeeId(employeeId);
    }
}